﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Ebc.DesignerDsl.TypeBrowserIntegration.Test
{
    [TestClass]
    public class ParseUsingsTest
    {
        [TestMethod]
        public void ParseSimpleType()
        {
            var result = new UsingsParser().ParseToCSharpString(typeof(string));
            Assert.AreEqual("string", result.CSharpType);
            Assert.AreEqual(1, result.Usings.Count());
            Assert.AreEqual("System", result.Usings.First());
        }

        [TestMethod]
        public void ParseComplexTupleOfCoreTypes()
        {
            var result = new UsingsParser().ParseToCSharpString(typeof(Tuple<string, string, Tuple<object, string, Tuple<string, int, Tuple<object>>>>));
            Assert.AreEqual("Tuple<string,string,Tuple<object,string,Tuple<string,int,Tuple<object>>>>", result.CSharpType);
            Assert.AreEqual(1, result.Usings.Count());
            Assert.AreEqual("System", result.Usings.First());
        }

        [TestMethod]
        public void ParseComplexTupleWithCustomTypes()
        {
            var result = new UsingsParser().ParseToCSharpString(typeof(Tuple<string, CustomDataContract, Tuple<object, string, Tuple<CustomDataContract, int, Tuple<object>>>>));
            Assert.AreEqual("Tuple<string,CustomDataContract,Tuple<object,string,Tuple<CustomDataContract,int,Tuple<object>>>>", result.CSharpType);
            Assert.AreEqual(2, result.Usings.Count());
            Assert.IsTrue(result.Usings.Contains("System"));
            Assert.IsTrue(result.Usings.Contains("Ebc.DesignerDsl.TypeBrowserIntegration.Test"));
        }

        [TestMethod]
        public void ParseTupleWithExtendedNamespaces()
        {
            var result = new UsingsParser().ParseToCSharpString(typeof(Tuple<System.Attribute, System.Text.Encoding, System.Timers.Timer, System.Threading.Barrier,
                System.Threading.Timer, System.Web.AspNetHostingPermission, System.IO.Stream>));
            Assert.AreEqual("Tuple<Attribute,Encoding,Timer,Barrier,Timer,AspNetHostingPermission,Stream>", result.CSharpType);
            Assert.AreEqual(6, result.Usings.Count());
            Assert.IsTrue(result.Usings.Contains("System"));
            Assert.IsTrue(result.Usings.Contains("System.Text"));
            Assert.IsTrue(result.Usings.Contains("System.Timers"));
            Assert.IsTrue(result.Usings.Contains("System.Threading"));
            Assert.IsTrue(result.Usings.Contains("System.Web"));
            Assert.IsTrue(result.Usings.Contains("System.IO"));
        }

        [TestMethod]
        public void ParseCustomGeneric()
        {
            var result = new UsingsParser().ParseToCSharpString(typeof(CustomGeneric<object>));
            Assert.AreEqual("CustomGeneric<object>", result.CSharpType);
            Assert.AreEqual(2, result.Usings.Count());
            Assert.IsTrue(result.Usings.Contains("System"));
            Assert.IsTrue(result.Usings.Contains("Ebc.DesignerDsl.TypeBrowserIntegration.Test"));
        }

        [TestMethod]
        public void ParseNestedType()
        {
            var result = new UsingsParser().ParseToCSharpString(typeof(CustomDataContract.CustomNestedDataContract));
            Assert.AreEqual("CustomDataContract.CustomNestedDataContract", result.CSharpType);
            Assert.AreEqual(1, result.Usings.Count());
            Assert.AreEqual("Ebc.DesignerDsl.TypeBrowserIntegration.Test", result.Usings.First());
        }
    }

    public class CustomGeneric<T>
    {
    }

    public class CustomDataContract
    {
        public class CustomNestedDataContract
        {
        }
    }
}